﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HeathCareSystem.Tier.DTO;
using HeathCareSystem.Tier.BUS;
using HeathCareSystem.Tier.DTO.View;

namespace HeathCareSystem.wuc
{
    public partial class wucThemBenhAn : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Fill_GV();
            Fill_Control();
        }

        protected void Fill_Control()
        {
            ddlChanDoan.DataTextField = "TenBenh";
            ddlChanDoan.DataValueField = "MaBenh";
            ddlChanDoan.DataSource = LoaiBenhBUS.DanhSachLoaiBenh();
            ddlChanDoan.DataBind();
        }
        protected void Fill_GV()
        {
            List<BenhNhanTrongNgayDTO> lst = BenhNhanTrongNgayBUS.DanhSachBenhNhanTrongNgay();
            List<BenhNhanTrongNgayV> lstv = new List<BenhNhanTrongNgayV>();
            foreach (BenhNhanTrongNgayDTO bn in lst)
            {
                lstv.Add(new BenhNhanTrongNgayV(bn));
            }
            gvThongTinBN.DataSource = lstv;
            gvThongTinBN.DataBind();
        }

        protected ChiTietBenhAnDTO LayThongTinTuNguoiDung()
        {
            ChiTietBenhAnDTO ctba = new ChiTietBenhAnDTO();
            try
            {
                ctba.maBenhNhan = txtMa.Text;
                ctba.ngayKham = DateTime.Parse(txtNgay.Text);
                ctba.trieuChung = txtTrieuChung.Text;
                ctba.ketLuan = txtKetLuan.Text;
                ctba.ngayTaiKham = DateTime.Parse(txtNgayTai.Text);
                // Lấy thông tin bệnh nhân trong ngày theo mã
                BenhNhanTrongNgayDTO bn = BenhNhanTrongNgayBUS.LayThongTinTheoMa(txtMa.Text);
                ctba.maNguoiDung = bn.maBacSi;
                ctba.maBenhNhan = bn.maBenhNhan;
                ctba.maChiTietBenhAn = bn.maChiTietBenhAn;
            }
            catch (Exception)
            {
                ctba = null;
            }
            return ctba;
        }

        // Thêm chi tiết bệnh án
        protected void btnThem_Click(object sender, EventArgs e)
        {
            int flag = 0;
            bool kq = false;
            ChiTietBenhAnDTO ctba = LayThongTinTuNguoiDung();
            if (ChiTietBenhAnBUS.KiemTraTonTai(ctba.maChiTietBenhAn))
            {
                ChiTietBenhAnBUS.CapNhat(ctba);
                flag = 1;
            }
            else
                kq = ChiTietBenhAnBUS.Them(ctba);
            ChanDoanBenhDTO cdb = new ChanDoanBenhDTO();
            cdb.maChiTietBenhAn = ctba.maChiTietBenhAn;
            cdb.maBenh = int.Parse(ddlChanDoan.SelectedValue);
            if (ChanDoanBenhBUS.KiemTraTonTai(cdb.maChiTietBenhAn))
                ChanDoanBenhBUS.CapNhat(cdb);
            else
                ChanDoanBenhBUS.Them(cdb);
            if (flag == 0)
            {
                if (kq)
                    lblThongBao.Text = "Thêm thành công!";
                else
                    lblThongBao.Text = "Thêm thất bại!";
            }
            else
            {
                lblThongBao.Text = "Cập nhật thành công";
            }
        }

        protected void btnThoat_Click(object sender, EventArgs e)
        {
            lblThongBao.Text = "";
            txtKetLuan.Text = "";
            txtMa.Text = "";
            txtNgay.Text = "";
            txtNgayTai.Text = "";
            txtTrieuChung.Text = "";
            ddlChanDoan.SelectedIndex = 0;
        }

        protected void gvThongTinBN_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void HienThiThongTin(BenhNhanTrongNgayDTO bn)
        {
            txtMa.Text = bn.soKhamBenh;
            BenhNhanTrongNgayDTO bntn = BenhNhanTrongNgayBUS.LayThongTinTheoMa(txtMa.Text);
            if (ChiTietBenhAnBUS.KiemTraTonTai(bntn.maChiTietBenhAn))
            {
                ChiTietBenhAnDTO ct = ChiTietBenhAnBUS.LayThongTinTuMa(bntn.maChiTietBenhAn);
                txtKetLuan.Text = ct.ketLuan;
                txtNgay.Text = string.Format("{0:MM/dd/yyyy}", ct.ngayKham);
                txtNgayTai.Text = string.Format("{0:MM/dd/yyyy}", ct.ngayTaiKham);
                txtTrieuChung.Text = ct.trieuChung;
                ddlChanDoan.SelectedValue = ChanDoanBenhBUS.LayThongTinTheoMa(bntn.maChiTietBenhAn).maBenh.ToString();
            }
        }

        protected void gvThongTinBN_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Dòng select
            int irow = Convert.ToInt32(e.CommandArgument);
            string SoKhamBenh = gvThongTinBN.Rows[irow].Cells[1].Text;
            // Lấy thông tin bệnh nhân trong ngày theo mã
            BenhNhanTrongNgayDTO bn = BenhNhanTrongNgayBUS.LayThongTinTheoMa(SoKhamBenh);
            HienThiThongTin(bn);
        }

        protected void gvThongTinBN_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvThongTinBN.PageIndex = e.NewPageIndex;
            Fill_GV();
        }

        protected void ddlLoc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}